<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	template="/templates/templatePrincipal.xhtml"
	xmlns:sec="http://www.springframework.org/security/facelets/tags">

	<ui:param name="mbean" value="${pesquisaAgendaMB}" />

	<ui:define name="content">
		<h:form>
			<h:panelGrid columns="2">
				<h:outputText value="Médico: " />
				<p:selectOneMenu value="#{mbean.medico}" converter="medicoConverter">
					<sec:ifAllGranted roles="ROLE_ADMIN">
						<f:selectItem itemLabel="Selecione" itemValue="#{null}" />
					</sec:ifAllGranted>
					<f:selectItems value="#{mbean.medicos}" var="medico"
						itemLabel="#{medico.funcionario.usuario.nome}" />
					<p:ajax event="change" update="@form" process="@this"
						listener="#{mbean.carregarAgendaMedico}"
						oncomplete="myschedule.update();" />
				</p:selectOneMenu>
			</h:panelGrid>

			<p:spacer height="4" />

			<p:outputPanel rendered="#{not empty mbean.medico}">
				<p:schedule value="#{mbean.eventModel}" locale="pt"
					timeZone="America/Sao_Paulo" widgetVar="myschedule">
					<p:ajax event="dateSelect"
						listener="#{pesquisaAgendaMB.onDateSelect}" oncomplete="novaAgenda();"/>
					<p:ajax event="eventSelect"
						listener="#{pesquisaAgendaMB.onEventSelect}" oncomplete="editarAgenda();"/>
				</p:schedule>
			</p:outputPanel>

			<p:remoteCommand name="editarAgenda"
				action="#{agendaMB.editarAgendamento(mbean.agenda)}"
				update=":formAgenda" oncomplete="agendaDialog.show();" />

			<p:remoteCommand name="novaAgenda"
				action="#{agendaMB.novoAgendamento(mbean.novaAgenda)}"
				update=":formAgenda" oncomplete="agendaDialog.show();" />

			<p:outputPanel rendered="#{empty mbean.medico}">
				<h2 style="color: red; font-family: serif;">Selecione um médico
					para visualizar sua agenda!</h2>
			</p:outputPanel>

			<p:growl id="messages" showDetail="true" />
		</h:form>

		<ui:decorate template="/sistema/agenda/modalAgenda.xhtml">
			<ui:define name="eventCloseDialog">
				<p:ajax event="close" listener="#{mbean.carregarAgendaMedico}"
					oncomplete="myschedule.update();" />
			</ui:define>
		</ui:decorate>
	</ui:define>
</ui:composition>
